/**
 * @author 徐楠
 * @date 2022/2/23 23:37
 * @version 1.0
 */

package com.xunan.likou;

public class SearchInsertionLocation {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        int target = 2;
        int result = searchInsert(nums, target);
        System.out.println(result);
    }

    public static int searchInsert(int[] nums, int target) {
        int length = nums.length;
        int left = 0;
        for (int i = 0; i < length; i++) {
            if (target == nums[i]) {
                return i;
            } else if (target > nums[i]) {
                if (i == length - 1) {
                    return i + 1;
                }
                left = i;
            } else {
                left = i;
                return left;
            }
        }
        return left;
    }
}
